package com.tj.jinzao.widgets;

import android.animation.Animator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewPropertyAnimator;

/**
 * 自定义上划控件继承NestedScrollView
 * 用于解决嵌套recyclerview的滑动问题
 * Created by wjy on 2018/5/4.
 */
public class MoveLayout extends NestedScrollView implements Animator.AnimatorListener {

    private float mStartY, mLastY;
    private int mRawLocation;
    private float mDownY;
    private int mHideY;
    private boolean mMove = true;

    public MoveLayout(Context context) {
        super(context);
    }

    public MoveLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MoveLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mRawLocation = (int) getTranslationY();
//        mHideY = (int) (30 * Constant.SCALE_Y);
    }

    public void setMove(boolean move) {
        mMove = move;
    }

    public void setLocation(int y) {
        setTranslationY(y);
        mRawLocation = y;
    }

    public void setHideY(int hideY) {
        this.mHideY = hideY;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mMove) return super.onTouchEvent(event);
        float transY = getTranslationY();
        float scrollY = getScrollY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownY = event.getRawY();
                mStartY = event.getRawY() - getTranslationY();
                mLastY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float offset = event.getRawY() - mStartY;
                boolean isUp = mLastY > event.getRawY();
                mLastY = event.getRawY();
                if(Math.abs(mLastY-mDownY)<40){
                    return true;
                }
                if (isUp) {
                    if (transY > -mHideY) {
                        setTranslationY(offset);
                    } else {
                        setTranslationY(-mHideY);
                        return super.onTouchEvent(event);
                    }
                } else {
                    if (scrollY > 20) {
                        return super.onTouchEvent(event);
                    } else {
                        setTranslationY(offset);
                    }
                }
                if (mListener != null) mListener.onMoved(getTranslationX(), getTranslationY());
                break;
            case MotionEvent.ACTION_UP:
                if (scrollY <= 20) {
                    float distance = event.getRawY() - mDownY;
                    ViewPropertyAnimator animator = animate();
                    animator.setListener(this);
                    if (distance > 0) {
                        animator.translationY(mRawLocation);
                    } else {
                        animator.translationY(-mHideY);
                    }
                } else {
                    return super.onTouchEvent(event);
                }
                break;
        }
        return true;
    }

    public void close() {
        animate().cancel();
        setTranslationY(mRawLocation);
    }

    public void closeByAnim() {
        animate().translationY(mRawLocation);
    }

    public void open() {
        animate().translationY(-mHideY);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (!mMove) return super.onInterceptTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownY = event.getRawY();
                mStartY = event.getRawY() - getTranslationY();
                mLastY = event.getRawY();
        }
        return super.onInterceptTouchEvent(event);
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (mListener != null) {
                mListener.onMoved(getTranslationX(), getTranslationY());
                mHandler.sendEmptyMessageDelayed(1, 5);
            }
            return true;
        }
    });

    @Override
    public void onAnimationStart(Animator animator) {
        if (mHandler != null) mHandler.sendEmptyMessage(1);
    }

    @Override
    public void onAnimationEnd(Animator animator) {
        if (mHandler != null) mHandler.removeMessages(1);

    }

    @Override
    public void onAnimationCancel(Animator animator) {
        if (mHandler != null) mHandler.removeMessages(1);
    }

    @Override
    public void onAnimationRepeat(Animator animator) {
    }

    public interface OnMoveListener {
        void onMoved(float transX, float transY);
    }

    private OnMoveListener mListener;

    public void setMoveListener(OnMoveListener listener) {
        this.mListener = listener;
    }
}
